package com.example.desksginktapp.viewmodels

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.baselibrary.utlis.LogUtils
import com.example.baselibrary.utlis.TimeHelpUtils
import com.example.httplibrary.beans.VisitorDataListResultBean
import com.example.httplibrary.https.ApiService
import com.example.httplibrary.https.NetworkUtils
import com.example.httplibrary.https.RetrofitClient
import com.xuexiang.xui.utils.XToastUtils
import com.zyf.baselibrary.base.BaseViewModel
import kotlinx.coroutines.launch

/**
 * @author 郑
 * @date 2025/8/4.
 * qq：331758871
 * phone：18979397268
 */
class VisitorySearchViewModel : BaseViewModel() {

    private val _visitorData = MutableLiveData<VisitorDataListResultBean.ResultDTO>()
    val visitorData: MutableLiveData<VisitorDataListResultBean.ResultDTO> = _visitorData
    fun visitorDataList(serachNum: String) {
        apiService = RetrofitClient.createService(ApiService::class.java)
        viewModelScope.launch {
            NetworkUtils.safeApiCallWithCallbacks(
                apiCall = {
                    apiService.visitorDataList(
                        1,
                        5,
                        TimeHelpUtils.getLocationTimeNew(TimeHelpUtils.FORMAT_DATE_EN),
                        serachNum
                    )
                },
                onSuccess = {
                    when {
                        it?.code == 200 && it.result.records.size > 0 -> {
                            _visitorData.value = it.result
                        }

                        it?.code == 200 -> {
                            XToastUtils.error("查询成功：当前查询码无数据")
                        }

                        else -> {
                            XToastUtils.error(it?.message ?: "查询失败")
                        }
                    }
                },
                onError = { code, message ->
                    XToastUtils.error(message)
                    LogUtils.logEvent(message)
                })
        }
    }
}
